﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using global::TerhelesEloszto.Common;

namespace Presentation
{
	public partial class Settings : System.Web.UI.Page
	{
		global::TerhelesEloszto.Engine.IEngine engine = global::TerhelesEloszto.Engine.Engine.Instance();
		String visitorName;
		protected void Page_Load( object sender, EventArgs e )
		{
			visitorName = User.Identity.Name;
			if( !engine.IsAdmin( visitorName ) )
			{
				if( engine.IsTeacher( visitorName ) )
				{
					Response.Redirect( "~/Teacher/TeacherProblem.aspx" );
				}
				else
				{
					Response.Redirect( "~/Problem.aspx" );
				}

			}
			if( Request.QueryString["msg"] != null )
			{
				if( Request.QueryString["msg"].Equals( "1" ) ) MessageLabel.Text = "A műveletet sikeresen végrehajtottuk!";
				else if( Request.QueryString["msg"].Equals( "0" ) ) MessageLabel.Text = "Hiba történt a művelet végrehajtása közben";
			}

			if( engine.CurrentSemester == global::TerhelesEloszto.Common.SemesterType.autumn && !Page.IsPostBack )
			{
				Autumn.Checked = true;
				Spring.Checked = false;

			}
			else if( engine.CurrentSemester == global::TerhelesEloszto.Common.SemesterType.spring && !Page.IsPostBack )
			{
				Autumn.Checked = false;
				Spring.Checked = true;

			}

			if( engine.IsTeacherSettingsEnabled && !Page.IsPostBack )
			{
				Enged.Checked = true;
				Tilt.Checked = false;

			}
			else if( !Page.IsPostBack )
			{
				Enged.Checked = false;
				Tilt.Checked = true;
			}
		}

		protected void SaveSemester_Click( object sender, EventArgs e )
		{
			if( engine.CurrentSemester == global::TerhelesEloszto.Common.SemesterType.autumn )
			{
				if( Spring.Checked == true )
				{
					if( engine.LoginUser( visitorName, engine.CreateHash( Passwd.Text ), false ) )
					{
						engine.SwitchSemester();
					}
					else
					{
						Response.Redirect( "~/Admin/Settings.aspx?msg=0" );
					}
				}
				Response.Redirect( "~/Admin/Settings.aspx?msg=1" );
			}
			else if( engine.CurrentSemester == global::TerhelesEloszto.Common.SemesterType.spring )
			{
				if( Autumn.Checked == true )
				{
					if( engine.LoginUser( visitorName, engine.CreateHash( Passwd.Text ), false ) )
					{
						engine.SwitchSemester();
					}
					else
					{
						Response.Redirect( "~/Admin/Settings.aspx?msg=0" );
					}
				}
				Response.Redirect( "~/Admin/Settings.aspx?msg=1" );
			}
		}

		protected void SaveSettingEnabled_Click( object sender, EventArgs e )
		{
			if( engine.IsTeacherSettingsEnabled )
			{
				if( Tilt.Checked == true )
				{
					if( engine.LoginUser( visitorName, engine.CreateHash( TextBox1.Text ), false ) )
					{
						engine.IsTeacherSettingsEnabled = false;
					}
					else
					{
						Response.Redirect( "~/Admin/Settings.aspx?msg=0" );
					}
				}
				Response.Redirect( "~/Admin/Settings.aspx?msg=1" );
			}
			else
			{
				if( Enged.Checked == true )
				{
					if( engine.LoginUser( visitorName, engine.CreateHash( TextBox1.Text ), false ) )
					{
						engine.IsTeacherSettingsEnabled = true;
					}
					else
					{
						Response.Redirect( "~/Admin/Settings.aspx?msg=0" );
					}
				}
				Response.Redirect( "~/Admin/Settings.aspx?msg=1" );
			}
		}
	}
}